/**
 * 深度优先搜索 当前菜单路径
 */
const getActivePathDFS = (
  activeKey: string,
  activePath: string[],
  menu?: Sys.Menu
) => {
  if (!menu) return false;

  if (activeKey == menu.key) return true;

  if (!menu.children) return false;

  for (const childMenu of menu.children as Sys.Menu[]) {
    if (getActivePathDFS(activeKey, activePath, childMenu)) {
      activePath.push(childMenu.key);
      return true;
    }
  }

  return false;
};

/**
 * 获取当前路由所在菜单数据的paths
 */
export const getActiveKeyOfMenus = (activeKey: string, menus: Sys.Menu[]) => {
  for (const menu of menus) {
    let keys: string[] = [];
    if (getActivePathDFS(activeKey, keys, menu)) {
      keys.push(menu.key);
      return keys;
    }
  }
  return [];
};
